package com.dshore.messagecenter.controller.technology;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dshore.messagecenter.controller.basedata.BaseController;
import com.dshore.messagecenter.util.WordEditOpenFileUtil;
import com.dshore.messagecenter.util.WordEditUtil;

@Controller
@RequestMapping("/index")
public class WordController extends BaseController{
	
	//编辑电子合同
	@RequestMapping("/toWord")
	public ModelAndView IntoChangeStyle() {
		ModelAndView mv=new ModelAndView("word");
		mv.setViewName("technology/word");
		return mv;
	}
	
	//编辑电子合同 将电子合同模版填充内容   基于java
	@RequestMapping(method = RequestMethod.POST, value ="/editWord")
	@ResponseBody
	public String editWord(String text1,String text2) {
		//生成doc
		System.out.println("text1:"+text1);
		System.out.println("text2:"+text2);
		Map<String,String> map=new HashMap<>();
        map.put("consignerClientName",text1);
        map.put("driverName",text2);
        map.put("orderId", "123456789");
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HHmmss");
        String time=sdf.format(d);
        //生成doc
        boolean a=WordEditUtil.getBuild("static/电子合同(司机).doc",map,"G:/aaa"+time+".doc");
        
        //生成docx
        boolean b=WordEditUtil.getBuildx("static/电子合同(司机).docx", map, "G:/bbb"+time+".docx");
        
        /*Map<String,Object> map1=new HashMap<>();
        map1.put("consignerClientName",text1);
        map1.put("driverName",text2);
        map1.put("orderId", "123456789");
        boolean c=new WordEditUtil().getBuildY("static/电子合同(司机).docx", map1, "G:/aaa"+time+".docx");*/
        
        if(a&&b) {
        	return "1";
        }else if(!a){
        	return "doc失败";
        }else {
        	return "docx失败";
        }
		
	}
	
	
	//电子文档  基于fileopen
	@RequestMapping(method = RequestMethod.POST, value ="/wordtopdf")
	@ResponseBody
	public String wordtopdf(String text1,String text2) throws FileNotFoundException {
        Map<String,String> map=new HashMap<>();
        map.put("consignerClientName",text1);
        map.put("driverName",text2);
        map.put("orderId", "123456789");
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HHmmss");
        String time=sdf.format(d);
        //电子文档填充为doc,把doc生成pdf
        try {
			String over = WordEditOpenFileUtil.getBuild("static/电子合同(司机).doc", map, "ccc"+time);
			System.out.println("pdf文件地址为："+over);
			return over;
		} catch (IOException e) {
			e.printStackTrace();
		}
        return "错误";
	}
}
